package cn.jetpiece.cloud.modular.doc;

import cn.jetpiece.cloud.config.properties.JetpieceGatewayProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * swagger聚合接口，三个接口都是 doc.html需要访问的接口
 */
@RestController
public class SwaggerResourceController {
    @Lazy
    @Autowired
    private SecurityConfiguration securityConfiguration;
    @Lazy
    @Autowired
    private UiConfiguration uiConfiguration;
    @Lazy
    @Autowired
    private JetpieceSwaggerResourceProvider swaggerResources;

    @GetMapping("/swagger-resources/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("/swagger-resources/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping("/swagger-resources")
    public Mono<ResponseEntity<List<SwaggerResource>>> swaggerResources() {
        if (JetpieceGatewayProperties.getInstance().getDoc()) {
            return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
        } else {
            return Mono.just((new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK)));
        }
    }
}
